package cn.com.pxto.web.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import cn.com.pxto.commons.httputil.ParamUtil;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.commons.pagesupport.PageUtils;
import cn.com.pxto.global.varables.LogonInfo;
import cn.com.pxto.model.User;
import cn.com.pxto.service.UserManager;

public class UserAction extends DispatchAction {
	private Log log  = LogFactory.getLog(UserAction.class);
	private UserManager userManager=null;
	
	public ActionForward preCreateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preCreateUser method");
		return mapping.findForward("preCreateUser");
	}
	
	public ActionForward doCreateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doCreateUser method");
//		type,deptId,password,userName,job,trueName,createDate
		DynaActionForm userForm = (DynaActionForm)form;
		User user = (User)userForm.get("user");
		boolean s=true;
		if(userManager.getUser(user.getUserName()) == null){
			user.setCreateDate(new Date().getTime());
			userManager.createUser(user);
		}else{
			request.setAttribute("message", "用户名被占用!");
			s=false;
		}
		request.setAttribute("boolean",s);
		request.getSession().getServletContext().setAttribute("userHasNew", true);
		return mapping.findForward("success");
	}
	
	public ActionForward preUpdateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preUpdateUser method");
		int uid=ParamUtil.getIntParams(request, "uid");
		User user=userManager.getUserById(uid);
		request.setAttribute("user", user);
		return mapping.findForward("preUpdateUser");
	}
	
	public ActionForward showUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in showUser method");
		int uid=ParamUtil.getIntParams(request, "uid");
		User user=userManager.getUserById(uid);
		request.setAttribute("user", user);
		return mapping.findForward("showUser");
	}
	
	public ActionForward doUpdateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doUpdateUser method");
		DynaActionForm userForm = (DynaActionForm)form;
		User user = (User)userForm.get("user");
		userManager.update(user);
//		return mapping.findForward("preListUser");
		request.getSession().getServletContext().setAttribute("userHasNew", true);
		return listUser(mapping,form,request,response);
	}
	
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in deleteUser method");
		int uid=ParamUtil.getIntParams(request, "uid");
		userManager.deleteUserById(uid);
//		return mapping.findForward("preListUser");
		return listUser(mapping,form,request,response);
	}
	
	public ActionForward listUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in listUser method");
		PageInfo pageInfo=PageUtils.getPageInfo(request);
		List<User> userList=userManager.listAllUser(pageInfo);
//		List userList=userManager.listAllUser(pageInfo);
		request.setAttribute("userList", userList);
		request.setAttribute("pageInfo", pageInfo);
		return mapping.findForward("listUser");
	}
	
	public ActionForward showMyself(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in showMyself method");
		LogonInfo logonInfo=(LogonInfo) request.getSession().getAttribute("logonInfo");
		User user=userManager.getUserById(logonInfo.getUserId());
		request.setAttribute("user", user);
		return mapping.findForward("showMyself");
	}
	
	public ActionForward doUpdateMyself(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doUpdateMyself method");
		DynaActionForm userForm = (DynaActionForm)form;
		User user = (User)userForm.get("user");
		userManager.update(user);
		return showMyself(mapping, form, request, response);
	}
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
}
